Docker Compose
Java + MongoDB + Redis
docker-compose.yml
version: "3" networks: network: driver: bridge services: redis: image: redis:6.0.6 container_name: myservice-redis ports: - "6379:6379" volumes: - ./config/redis/redis.conf:/etc/redis/redis.conf - redisdata:/data networks: - network mongo: image: mongo:5.0.9 container_name: myservice-mongo restart: always ports: - "27017:27017" environment: MONGO_SAVE_PATH: /data/db TZ: Asia/Shanghai MONGO_INITDB_ROOT_USERNAME: "root" MONGO_INITDB_ROOT_PASSWORD: "root" MONGO_INITDB_DATABASE: mydatabase MONGO_USERNAME: username MONGO_PASSWORD: password volumes: - mongodata:/data/db - ./config/mongo:/docker-entrypoint-initdb.d networks: - network myapp: restart: always build: context: ./myapp dockerfile: Dockerfile image: myapp:1.0 container_name: myapp ports: - 8080:8080 environment: TZ: Asia/Shanghai networks: - network volumes: mongodata: redisdata:
mongo-init.sh
mongo -- "$MONGO_INITDB_DATABASE" <<EOF db = db.getSiblingDB('admin') db.auth('$MONGO_INITDB_ROOT_USERNAME', '$MONGO_INITDB_ROOT_PASSWORD') db = db.getSiblingDB('$MONGO_INITDB_DATABASE') db.createUser({user: "$MONGO_USERNAME",pwd: "$MONGO_PASSWORD",roles: [{role: 'readWrite',db: '$MONGO_INITDB_DATABASE'}]}) EOF
redis.conf
# 修改连接为所有ip bind 0.0.0.0 # 允许外网访问 protected-mode no port 6379 timeout 0 # RDB存储配置 save 900 1 save 300 10 save 60 10000 rdbcompression yes dbfilename dump.rdb # 数据存放位置 dir /data # 开启aof配置 appendonly yes appendfsync everysec appendfilename "appendonly.aof" # 设置密码 # requirepass 123456